home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / util / sys / AllocP.lha / AllocP / AllocP.c < prev    next >
C/C++ Source or Header  |  1997-08-24  |  7KB  |  209 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *  AllocP - supply a better AllocMem/AllocVec                          *
  4.  *                                                                      *
  5.  *  written by Andreas R. Kleinert                                      *
  6.  *  Andreas_Kleinert@t-online.de                                        *
  7.  *                                                                      *
  8.  *  No Assembler - 100 percent C                                        *
  9.  *                                                                      *
  10.  * V1.3 :                                                               *
  11.  *        - when the real "avail flush" process took place,             *
  12.  *          a memory loss could have occured                            *
  13.  *        - added Forbid/Permit                                         *
  14.  *        - fixed/changed some other stuff                              *
  15.  *                                                                      *
  16.  * V1.2 :                                                               *
  17.  *        - redone with SAS/C 6.58                                      *
  18.  *        - now checks for MEMF_NO_EXPUNGE                              *
  19.  *        - utility.library was not needed                              *
  20.  *        - fixed requester layout                                      *
  21.  *        - fixed #include list                                         *
  22.  *        - now 68020 compiled (040 before, thus 020 ;-)                *
  23.  *        - fixed $VERsion string                                       *
  24.  *        - etc.                                                        *
  25.  *        - saved > 400 Bytes                                           *
  26.  *                                                                      *
  27.  * V1.1 :                                                               *
  28.  *        - V37 works, too                                              *
  29.  *        - alignment feature was broken                                *
  30.  *        - reduced stack size                                          *
  31.  *                                                                      *
  32.  * V1.0 :                                                               *
  33.  *        - first release                                               *
  34.  *                                                                      *
  35.  ************************************************************************/
  36.  
  37. #define __USE_SYSBASE
  38.  
  39. #include <exec/execbase.h>
  40. #include <exec/memory.h>
  41. #include <intuition/intuition.h>
  42.  
  43. #include <string.h>
  44. #include <stdio.h>
  45.  
  46. #include <proto/exec.h>
  47. #include <proto/dos.h>
  48. #include <proto/intuition.h>
  49.  
  50. #define N (NULL)
  51.  
  52.  
  53. extern struct ExecBase *SysBase;
  54.  
  55. struct IntuitionBase *IntuitionBase = N;
  56.  
  57.  
  58. APTR __far NewAllocMemVec;
  59. ULONG __far __asm (*OrigAllocMemVec)(register __d0 ULONG size,
  60.                                      register __d1 ULONG memf,
  61.                                      register __a6 struct ExecBase *ExecBase_a6);
  62.  
  63. APTR __far NewAllocVecVec;
  64. ULONG __far __asm (*OrigAllocVecVec)(register __d0 ULONG size,
  65.                                      register __d1 ULONG memf,
  66.                                      register __a6 struct ExecBase *ExecBase_a6);
  67.  
  68.  
  69. ULONG __asm NewAllocMem(register __d0 ULONG size,
  70.                         register __d1 ULONG memf,
  71.                         register __a6 struct ExecBase *ExecBase_a6)
  72. {
  73.  ULONG ptr;
  74.  
  75.  ptr = OrigAllocMemVec(size, memf, ExecBase_a6);
  76.  if(!ptr)
  77.   {
  78.    if(size <= 0xFFFFFF00) /* otherwise this was a "flush" call, only */
  79.     {
  80.      if(!(memf & MEMF_NO_EXPUNGE)) OrigAllocMemVec((ULONG) 0xFFFFFFFF, MEMF_ANY, ExecBase_a6);
  81.  
  82.      ptr = OrigAllocMemVec(size, memf, ExecBase_a6);
  83.     }
  84.   }
  85.  
  86.  return((ULONG) ptr);
  87. }
  88.  
  89. ULONG __asm NewAllocVec(register __d0 ULONG size,
  90.                         register __d1 ULONG memf,
  91.                         register __a6 struct ExecBase *ExecBase_a6)
  92. {
  93.  ULONG ptr, nsize, gap;
  94.  
  95.  if(gap = ((nsize=size) & 3))
  96.   {
  97.                     nsize = size + (4 - gap); /* avoid alignment gaps */
  98.    if(size > nsize) nsize = size;             /* we produced an overflow */
  99.   }
  100.  
  101.  ptr = OrigAllocVecVec(nsize, memf, ExecBase_a6);
  102.  if(!ptr)
  103.   {
  104.    if(size <= 0xFFFFFF00) /* otherwise this was a "flush" call, only */
  105.     {
  106.      if(!(memf & MEMF_NO_EXPUNGE)) OrigAllocMemVec((ULONG) 0xFFFFFFFF, MEMF_ANY, ExecBase_a6);
  107.  
  108.      ptr = OrigAllocVecVec(nsize, memf, ExecBase_a6);
  109.     }
  110.   }
  111.  
  112.  return((ULONG) ptr);
  113. }
  114.  
  115. /* *************************************************** */
  116. /* *                                                 * */
  117. /* * Compiler Stuff for BackgroundIO                 * */
  118. /* *                                                 * */
  119. /* *************************************************** */
  120.  
  121. long  __stack        = 1024;
  122. char *__procname     = "AllocP";
  123. long  __priority     = 1;
  124. long  __BackGroundIO = 1;
  125.  
  126. void __regargs __chkabort(void) { }
  127. void __regargs _CXBRK(void)     { }
  128.  
  129. char vertext [] = "\0$VER: AllocP 1.1 (16.8.97)";
  130.  
  131.  
  132. long main(long argc, char **argv)
  133. {
  134.  long retval = 0;
  135.  
  136.  APTR task;
  137.  
  138.  Forbid();
  139.  task = FindTask(N);
  140.  Permit();
  141.  
  142.  if(task) SetTaskPri(task, __priority);
  143.  
  144.  
  145.  IntuitionBase = (APTR) OpenLibrary("intuition.library", 37);
  146.  if(IntuitionBase)
  147.   {
  148.    Forbid();
  149.    OrigAllocMemVec = (long (* __asm )(register __d0 ULONG size,
  150.                                       register __d1 ULONG memf,
  151.                                       register __a6 struct ExecBase *ExecBase_a6)) SetFunction((APTR)SysBase, -0xc6, (APTR)NewAllocMem);
  152.  
  153.    OrigAllocVecVec = (long (* __asm )(register __d0 ULONG size,
  154.                                       register __d1 ULONG memf,
  155.                                       register __a6 struct ExecBase *ExecBase_a6)) SetFunction((APTR)SysBase, -0x2ac, (APTR)NewAllocVec);
  156.    Permit();
  157.  
  158.    if(OrigAllocMemVec || OrigAllocVecVec)
  159.     {
  160.      if(task) SetTaskPri(task, -10);
  161.      Wait(SIGBREAKF_CTRL_C);
  162.      if(task) SetTaskPri(task, 1);
  163.  
  164.      Forbid();
  165.      NewAllocMemVec= (APTR) SetFunction((APTR)SysBase, -0xc6,  (APTR)OrigAllocMemVec);
  166.      NewAllocVecVec= (APTR) SetFunction((APTR)SysBase, -0x2ac, (APTR)OrigAllocVecVec);
  167.      Permit();
  168.  
  169.      if(NewAllocMemVec!=NewAllocMem)
  170.       {
  171.        ULONG idcmp = N;
  172.        struct EasyStruct estr;
  173.  
  174.        estr.es_StructSize   = sizeof(struct EasyStruct);
  175.        estr.es_Flags        = N;
  176.        estr.es_Title        = "AllocP Request";
  177.        estr.es_TextFormat   = "Error while removing from system !\n"
  178.                               "There were more patches !\n"
  179.                               "Do a reset soon !";
  180.        estr.es_GadgetFormat = "Ok";
  181.  
  182.        EasyRequestArgs(N, &estr, (ULONG *) &idcmp, N);
  183.       }
  184.  
  185.      if(NewAllocVecVec!=NewAllocVec)
  186.       {
  187.        ULONG idcmp = N;
  188.        struct EasyStruct estr;
  189.  
  190.        estr.es_StructSize   = sizeof(struct EasyStruct);
  191.        estr.es_Flags        = N;
  192.        estr.es_Title        = "AllocP Request";
  193.        estr.es_TextFormat   = "Error while removing from system !\n"
  194.                               "There were more patches !\n"
  195.                               "Do a reset soon !";
  196.        estr.es_GadgetFormat = "Ok";
  197.  
  198.        EasyRequestArgs(N, &estr, (ULONG *) &idcmp, N);
  199.       }
  200.  
  201.     }else retval = 20;
  202.  
  203.    CloseLibrary((struct Library *) IntuitionBase);
  204.  
  205.   }else retval = 20;
  206.  
  207.  return(retval);
  208. }
  209.